<?php

namespace App\Console\Commands\Users;

use Illuminate\Console\Command;
use App\Models\User;

class ListUsersCommand extends Command
{
  /**
   * The name and signature of the console command.
   *
   * @var string
   */
  protected $signature = 'users:list';

  /**
   * The console command description.
   *
   * @var string
   */
  protected $description = 'List all users in the database';

  /**
   * Execute the console command.
   */
  public function handle()
  {
    $users = User::all();

    if ($users->isEmpty()) {
      $this->info('No users found in the database.');
      return;
    }

    $headers = ['ID', 'Name', 'Email', 'Created At'];
    $data = [];

    foreach ($users as $user) {
      $data[] = [
        $user->id,
        $user->name,
        $user->email,
        $user->created_at->format('Y-m-d H:i:s')
      ];
    }

    $this->table($headers, $data);
  }
}
